package org.epidroid.epidroid.androsoul.ui.contactlist;

import java.util.Iterator;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.androsoul.engine.contacts.ContactGroup;
import org.epidroid.epidroid.androsoul.engine.contacts.Location;
import org.epidroid.epidroid.androsoul.engine.contacts.NetsoulContact;
import org.epidroid.epidroid.androsoul.engine.service.IAndroSoulService;
import org.epidroid.epidroid.androsoul.engine.utils.StringUtils;
import org.epidroid.epidroid.androsoul.ui.AndroSoulActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowContactHandler {

	IAndroSoulService _service = null;
	AndroSoulActivity _context = null;

	public ShowContactHandler(IAndroSoulService service,
			AndroSoulActivity context) {
		_service = service;
		_context = context;
	}

	public void showContact(final NetsoulContact c, final ContactGroup g) {
		AlertDialog.Builder alert = new AlertDialog.Builder(_context);
		if ((c.getState() == null) || (c.getState().equals("")))
			alert.setTitle(c.getName());
		else
			alert.setTitle(c.getName() + " (" + c.getState() + ")");
		LayoutInflater factory = LayoutInflater.from(_context);
		final View alertDialogView = factory.inflate(
				R.layout.androsoul_show_contact, null);
		alert.setView(alertDialogView);

		setContactDescription(c, alertDialogView);
		setContactImage(c, alertDialogView);

		alert.setPositiveButton(R.string.close, null);
		alert.setNeutralButton(R.string.groups, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				_context.showDialogChangeGroup(c);
			}
		});
		alert.setNegativeButton(R.string.delete, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				_service.deleteContact(c, g);
				_context.updateContactsListAdapter();
				_context.saveContacts();
			}
		});
		alert.show();
	}

	private void setContactDescription(NetsoulContact c, View alertDialogView) {
		TextView txt = (TextView) alertDialogView
				.findViewById(R.id.show_contact_content);

		if (c.getLocations().isEmpty()) {
			txt.setText(c.getName() + " " + _context.getString(R.string.not_connected));
			return;
		}

		String content = "";
		if (c.getComment() != null)
			content = "«" + StringUtils.urlDecode(c.getComment()) + "»\n\n";

		content += "Locations:";
		Iterator<Location> it = c.locIterator();
		while (it.hasNext()) {
			Location l = it.next();
			content += "\n" + StringUtils.urlDecode(l.getBestLocation());
			content += " - " + l.getIp();
		}
		txt.setText(content);
	}

	private void setContactImage(NetsoulContact c, View alertDialogView) {
		ImageView img = (ImageView) alertDialogView
				.findViewById(R.id.show_contact_image);
		Drawable tmp = c.getImage(_context, false);
		if (tmp != null)
			img.setImageDrawable(tmp);
		else
			img.setImageResource(R.drawable.contact_picture_2);
	}
}
